home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / mint / shells / tcshsrc.zoo / tcsh / sh.lex.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-02-21  |  33.0 KB  |  1,663 lines

  1. /* $Header: /home/hyperion/mu/christos/src/sys/tcsh-6.00/RCS/sh.lex.c,v 3.3 1991/07/24 17:38:12 christos Exp $ */
  2. /*
  3.  * sh.lex.c: Lexical analysis into tokens
  4.  */
  5. /*-
  6.  * Copyright (c) 1980, 1991 The Regents of the University of California.
  7.  * All rights reserved.
  8.  *
  9.  * Redistribution and use in source and binary forms, with or without
  10.  * modification, are permitted provided that the following conditions
  11.  * are met:
  12.  * 1. Redistributions of source code must retain the above copyright
  13.  *    notice, this list of conditions and the following disclaimer.
  14.  * 2. Redistributions in binary form must reproduce the above copyright
  15.  *    notice, this list of conditions and the following disclaimer in the
  16.  *    documentation and/or other materials provided with the distribution.
  17.  * 3. All advertising materials mentioning features or use of this software
  18.  *    must display the following acknowledgement:
  19.  *    This product includes software developed by the University of
  20.  *    California, Berkeley and its contributors.
  21.  * 4. Neither the name of the University nor the names of its contributors
  22.  *    may be used to endorse or promote products derived from this software
  23.  *    without specific prior written permission.
  24.  *
  25.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  26.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  27.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  28.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  29.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  30.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  31.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  32.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  33.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  34.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  35.  * SUCH DAMAGE.
  36.  */
  37. #include "config.h"
  38. RCSID("$Id: sh.lex.c,v 3.3 1991/07/24 17:38:12 christos Exp $")
  39.  
  40. #include "sh.h"
  41. #include "ed.h"
  42.  
  43. /*
  44.  * C shell
  45.  */
  46.  
  47. /*
  48.  * These lexical routines read input and form lists of words.
  49.  * There is some involved processing here, because of the complications
  50.  * of input buffering, and especially because of history substitution.
  51.  */
  52. static    Char        *word        __P((void));
  53. static    int          getC1        __P((int));
  54. static    void          getdol        __P((void));
  55. static    void          getexcl    __P((int));
  56. static    struct Hist     *findev        __P((Char *, bool));
  57. static    void          setexclp    __P((Char *));
  58. static    int          bgetc        __P((void));
  59. static    void          bfree        __P((void));
  60. static    struct wordent    *gethent    __P((int));
  61. static    int          matchs        __P((Char *, Char *));
  62. static    int          getsel        __P((int *, int *, int));
  63. static    struct wordent    *getsub        __P((struct wordent *));
  64. static    Char         *subword    __P((Char *, int, bool *));
  65. static    struct wordent    *dosub        __P((int, struct wordent *, bool));
  66.  
  67. /*
  68.  * Peekc is a peek character for getC, peekread for readc.
  69.  * There is a subtlety here in many places... history routines
  70.  * will read ahead and then insert stuff into the input stream.
  71.  * If they push back a character then they must push it behind
  72.  * the text substituted by the history substitution.  On the other
  73.  * hand in several places we need 2 peek characters.  To make this
  74.  * all work, the history routines read with getC, and make use both
  75.  * of ungetC and unreadc.  The key observation is that the state
  76.  * of getC at the call of a history reference is such that calls
  77.  * to getC from the history routines will always yield calls of
  78.  * readc, unless this peeking is involved.  That is to say that during
  79.  * getexcl the variables lap, exclp, and exclnxt are all zero.
  80.  *
  81.  * Getdol invokes history substitution, hence the extra peek, peekd,
  82.  * which it can ungetD to be before history substitutions.
  83.  */
  84. static Char peekc = 0, peekd = 0;
  85. static Char peekread = 0;
  86.  
  87. /* (Tail of) current word from ! subst */
  88. static Char *exclp = NULL;
  89.  
  90. /* The rest of the ! subst words */
  91. static struct wordent *exclnxt = NULL;
  92.  
  93. /* Count of remaining words in ! subst */
  94. static int exclc = 0;
  95.  
  96. /* "Globp" for alias resubstitution */
  97. static Char *alvecp = NULL;
  98.  
  99. /*
  100.  * Labuf implements a general buffer for lookahead during lexical operations.
  101.  * Text which is to be placed in the input stream can be stuck here.
  102.  * We stick parsed ahead $ constructs during initial input,
  103.  * process id's from `$$', and modified variable values (from qualifiers
  104.  * during expansion in sh.dol.c) here.
  105.  */
  106. static Char labuf[BUFSIZ];
  107.  
  108. /*
  109.  * Lex returns to its caller not only a wordlist (as a "var" parameter)
  110.  * but also whether a history substitution occurred.  This is used in
  111.  * the main (process) routine to determine whether to echo, and also
  112.  * when called by the alias routine to determine whether to keep the
  113.  * argument list.
  114.  */
  115. static bool hadhist = 0;
  116.  
  117. #ifdef ALTESC
  118. /*
  119.  * this is the actual escape character (normally hardwired to '\\'),
  120.  * which we allow changing via "set histchar=^", for example.
  121.  */
  122. Char escchar = '\\';
  123. #endif
  124.  
  125. /*
  126.  * Avoid alias expansion recursion via \!#
  127.  */
  128. int     hleft;
  129.  
  130. Char    histline[BUFSIZ + 2];    /* last line input */
  131.  
  132.  /* The +2 is to fool hp's optimizer */
  133. bool    histvalid = 0;        /* is histline valid */
  134. static Char *histlinep = NULL;    /* current pointer into histline */
  135.  
  136. static Char getCtmp;
  137.  
  138. #define getC(f)        ((getCtmp = peekc) ? (peekc = 0, getCtmp) : getC1(f))
  139. #define    ungetC(c)    peekc = c
  140. #define    ungetD(c)    peekd = c
  141.  
  142. int
  143. lex(hp)
  144.     register struct wordent *hp;
  145. {
  146.     register struct wordent *wdp;
  147.     int     c;
  148.  
  149.     histvalid = 0;
  150.     histlinep = histline;
  151.     *histlinep = '\0';
  152.  
  153.     lineloc = btell();
  154.     hp->next = hp->prev = hp;
  155.     hp->word = STRNULL;
  156.     alvecp = 0, hadhist = 0;
  157.     do
  158.     c = readc(0);
  159.     while (c == ' ' || c == '\t');
  160.  
  161.     if (c == HISTSUB && intty)
  162.     /* ^lef^rit    from tty is short !:s^lef^rit */
  163.     getexcl(c);
  164.     else
  165.     unreadc(c);
  166.     wdp = hp;
  167.     /*
  168.      * The following loop is written so that the links needed by freelex will
  169.      * be ready and rarin to go even if it is interrupted.
  170.      */
  171.     do {
  172.     register struct wordent *new;
  173.  
  174.     new = (struct wordent *) xmalloc((size_t) sizeof(*wdp));
  175.     new->word = 0;
  176.     new->prev = wdp;
  177.     new->next = hp;
  178.     wdp->next = new;
  179.     wdp = new;
  180.     wdp->word = word();
  181.     } while (wdp->word[0] != '\n');
  182.     hp->prev = wdp;
  183.     if (histlinep < histline + BUFSIZ) {
  184.     *histlinep = '\0';
  185.     if (histlinep > histline && histlinep[-1] == '\n')
  186.         histlinep[-1] = '\0';
  187.     histvalid = 1;
  188.     }
  189.     else {
  190.     histline[BUFSIZ - 1] = '\0';
  191.     }
  192.     return (hadhist);
  193. }
  194.  
  195. void
  196. prlex(sp0)
  197.     struct wordent *sp0;
  198. {
  199.     register struct wordent *sp = sp0->next;
  200.  
  201.     for (;;) {
  202.     xprintf("%s", short2str(sp->word));
  203.     sp = sp->next;
  204.     if (sp == sp0)
  205.         break;
  206.     if (sp->word[0] != '\n')
  207.         xputchar(' ');
  208.     }
  209. }
  210.  
  211. void
  212. copylex(hp, fp)
  213.     register struct wordent *hp;
  214.     register struct wordent *fp;
  215. {
  216.     register struct wordent *wdp;
  217.  
  218.     wdp = hp;
  219.     fp = fp->next;
  220.     do {
  221.     register struct wordent *new;
  222.     
  223.     new = (struct wordent *) xmalloc((size_t) sizeof(*wdp));
  224.     new->prev = wdp;
  225.     new->next = hp;
  226.     wdp->next = new;
  227.     wdp = new;
  228.     wdp->word = Strsave(fp->word);
  229.     fp = fp->next;
  230.     } while (wdp->word[0] != '\n');
  231.     hp->prev = wdp;
  232. }
  233.  
  234. void
  235. freelex(vp)
  236.     register struct wordent *vp;
  237. {
  238.     register struct wordent *fp;
  239.  
  240.     while (vp->next != vp) {
  241.     fp = vp->next;
  242.     vp->next = fp->next;
  243.     xfree((ptr_t) fp->word);
  244.     xfree((ptr_t) fp);
  245.     }
  246.     vp->prev = vp;
  247. }
  248.  
  249. static Char *
  250. word()
  251. {
  252.     register Char c, c1;
  253.     register Char *wp;
  254.     Char    wbuf[BUFSIZ];
  255.     register bool dolflg;
  256.     register int i;
  257.  
  258.     wp = wbuf;
  259.     i = BUFSIZ - 4;
  260. loop:
  261.     while ((c = getC(DOALL)) == ' ' || c == '\t');
  262.     if (cmap(c, _META | _ESC))
  263.     switch (c) {
  264.     case '&':
  265.     case '|':
  266.     case '<':
  267.     case '>':
  268.         *wp++ = c;
  269.         c1 = getC(DOALL);
  270.         if (c1 == c)
  271.         *wp++ = c1;
  272.         else
  273.         ungetC(c1);
  274.         goto ret;
  275.  
  276.     case '#':
  277.         if (intty)
  278.         break;
  279.         c = 0;
  280.         do {
  281.         c1 = c;
  282.         c = getC(0);
  283.         } while (c != '\n');
  284.         if (c1 == escchar)
  285.         goto loop;
  286.         /* fall into ... */
  287.  
  288.     case ';':
  289.     case '(':
  290.     case ')':
  291.     case '\n':
  292.         *wp++ = c;
  293.         goto ret;
  294.  
  295. #ifdef ALTESC
  296.     default:
  297.         if (c != escchar) break;
  298. #else
  299.     case '\\':
  300. #endif
  301.         c = getC(0);
  302.         if (c == '\n') {
  303.         if (onelflg == 1)
  304.             onelflg = 2;
  305.         goto loop;
  306.         }
  307.         if (c != HIST)
  308.         *wp++ = escchar, --i;
  309.         c |= QUOTE;
  310.     }
  311.     c1 = 0;
  312.     dolflg = DOALL;
  313.     for (;;) {
  314.     if (c1) {
  315.         if (c == c1) {
  316.         c1 = 0;
  317.         dolflg = DOALL;
  318.         }
  319.         else if (c == escchar) {
  320.         c = getC(0);
  321. /*
  322.  * PWP: this is dumb, but how all of the other shells work.  If \ quotes
  323.  * a character OUTSIDE of a set of ''s, why shouldn't it quote EVERY
  324.  * following character INSIDE a set of ''s.
  325.  *
  326.  * Actually, all I really want to be able to say is 'foo\'bar' --> foo'bar
  327.  */
  328.         if (c == HIST)
  329.             c |= QUOTE;
  330.         else {
  331.             if (bslash_quote &&
  332.             ((c == '\'') || (c == '"') ||
  333.              (c == escchar))) {
  334.             c |= QUOTE;
  335.             }
  336.             else {
  337.             if (c == '\n')
  338.                 /*
  339.                  * if (c1 == '`') c = ' '; else
  340.                  */
  341.                 c |= QUOTE;
  342.             ungetC(c);
  343.             c = escchar;
  344.             }
  345.         }
  346.         }
  347.         else if (c == '\n') {
  348.         seterror(ERR_UNMATCHED, c1);
  349.         ungetC(c);
  350.         break;
  351.         }
  352.     }
  353.     else if (cmap(c, _META | _Q | _Q1 | _ESC)) {
  354.         if (c == escchar) {
  355.         c = getC(0);
  356.         if (c == '\n') {
  357.             if (onelflg == 1)
  358.             onelflg = 2;
  359.             break;
  360.         }
  361.         if (c != HIST)
  362.             *wp++ = escchar, --i;
  363.         c |= QUOTE;
  364.         }
  365.         else if (cmap(c, _Q | _Q1)) {    /* '"` */
  366.         c1 = c;
  367.         dolflg = c == '"' ? DOALL : DOEXCL;
  368.         }
  369.         else if (c != '#' || !intty) {
  370.         ungetC(c);
  371.         break;
  372.         }
  373.     }
  374.     if (--i > 0) {
  375.         *wp++ = c;
  376.         c = getC(dolflg);
  377.     }
  378.     else {
  379.         seterror(ERR_WTOOLONG);
  380.         wp = &wbuf[1];
  381.         break;
  382.     }
  383.     }
  384. ret:
  385.     *wp = 0;
  386.     return (Strsave(wbuf));
  387. }
  388.  
  389. static int
  390. getC1(flag)
  391.     register int flag;
  392. {
  393.     register Char c;
  394.  
  395.     while (1) {
  396.     if (c = peekc) {
  397.         peekc = 0;
  398. #ifdef __MINT__
  399.         return (c == '\r' ? getC1(flag) : c);
  400. #else
  401.         return (c);
  402. #endif
  403.     }
  404.     if (lap) {
  405.         if ((c = *lap++) == 0)
  406.         lap = 0;
  407.         else {
  408.         if (cmap(c, _META | _Q | _Q1))
  409.             c |= QUOTE;
  410. #ifdef __MINT__
  411.         return (c == '\r' ? getC1(flag) : c);
  412. #else
  413.         return (c);
  414. #endif
  415.         }
  416.     }
  417.     if (c = peekd) {
  418.         peekd = 0;
  419. #ifdef __MINT__
  420.         return (c == '\r' ? getC1(flag) : c);
  421. #else
  422.         return (c);
  423. #endif
  424.     }
  425.     if (exclp) {
  426.         if (c = *exclp++)
  427. #ifdef __MINT__
  428.         return (c == '\r' ? getC1(flag) : c);
  429. #else
  430.         return (c);
  431. #endif
  432.         if (exclnxt && --exclc >= 0) {
  433.         exclnxt = exclnxt->next;
  434.         setexclp(exclnxt->word);
  435.         return (' ');
  436.         }
  437.         exclp = 0;
  438.         exclnxt = 0;
  439.     }
  440.     if (exclnxt) {
  441.         exclnxt = exclnxt->next;
  442.         if (--exclc < 0)
  443.         exclnxt = 0;
  444.         else
  445.         setexclp(exclnxt->word);
  446.         continue;
  447.     }
  448.     c = readc(0);
  449.     if (c == '$' && (flag & DODOL)) {
  450.         getdol();
  451.         continue;
  452.     }
  453.     if (c == HIST && (flag & DOEXCL)) {
  454.         getexcl(0);
  455.         continue;
  456.     }
  457.     break;
  458.     }
  459.     return (c);
  460. }
  461.  
  462. static void
  463. getdol()
  464. {
  465.     register Char *np, *ep;
  466.     Char    name[4 * MAXVARLEN + 1];
  467.     register int c;
  468.     int     sc;
  469.     bool    special = 0, toolong;
  470.  
  471.     np = name, *np++ = '$';
  472.     c = sc = getC(DOEXCL);
  473.     if (any("\t \n", c)) {
  474.     ungetD(c);
  475.     ungetC('$' | QUOTE);
  476.     return;
  477.     }
  478.     if (c == '{')
  479.     *np++ = c, c = getC(DOEXCL);
  480.     if (c == '#' || c == '?')
  481.     special++, *np++ = c, c = getC(DOEXCL);
  482.     *np++ = c;
  483.     switch (c) {
  484.  
  485.     case '<':
  486.     case '$':
  487.     if (special)
  488.         seterror(ERR_SPDOLLT);
  489.     *np = 0;
  490.     addla(name);
  491.     return;
  492.  
  493.     case '\n':
  494.     ungetD(c);
  495.     np--;
  496.     seterror(ERR_NEWLINE);
  497.     *np = 0;
  498.     addla(name);
  499.     return;
  500.  
  501.     case '*':
  502.     if (special)
  503.         seterror(ERR_SPSTAR);
  504.     *np = 0;
  505.     addla(name);
  506.     return;
  507.  
  508.     default:
  509.     toolong = 0;
  510.     if (Isdigit(c)) {
  511. #ifdef notdef
  512.         /* let $?0 pass for now */
  513.         if (special) {
  514.         seterror(ERR_DIGIT);
  515.         *np = 0;
  516.         addla(name);
  517.         return;
  518.         }
  519. #endif
  520.         /* we know that np < &name[4] */
  521.         ep = &np[MAXVARLEN];
  522.         while (c = getC(DOEXCL)) {
  523.         if (!Isdigit(c))
  524.             break;
  525.         if (np < ep)
  526.             *np++ = c;
  527.         else
  528.             toolong = 1;
  529.         }
  530.     }
  531.     else if (letter(c)) {
  532.         /* we know that np < &name[4] */
  533.         ep = &np[MAXVARLEN];
  534.         toolong = 0;
  535.         while (c = getC(DOEXCL)) {
  536.         /* Bugfix for ${v123x} from Chris Torek, DAS DEC-90. */
  537.         if (!letter(c) && !Isdigit(c))
  538.             break;
  539.         if (np < ep)
  540.             *np++ = c;
  541.         else
  542.             toolong = 1;
  543.         }
  544.     }
  545.     else {
  546.         *np = 0;
  547.         seterror(ERR_VARILL);
  548.         addla(name);
  549.         return;
  550.     }
  551.     if (toolong) {
  552.         seterror(ERR_VARTOOLONG);
  553.         *np = 0;
  554.         addla(name);
  555.         return;
  556.     }
  557.     break;
  558.     }
  559.     if (c == '[') {
  560.     *np++ = c;
  561.     /*
  562.      * Name up to here is a max of MAXVARLEN + 8.
  563.      */
  564.     ep = &np[2 * MAXVARLEN + 8];
  565.     do {
  566.         /*
  567.          * Michael Greim: Allow $ expansion to take place in selector
  568.          * expressions. (limits the number of characters returned)
  569.          */
  570.         c = getC(DOEXCL | DODOL);
  571.         if (c == '\n') {
  572.         ungetD(c);
  573.         np--;
  574.         seterror(ERR_NLINDEX);
  575.         *np = 0;
  576.         addla(name);
  577.         return;
  578.         }
  579.         if (np < ep)
  580.         *np++ = c;
  581.     } while (c != ']');
  582.     *np = '\0';
  583.     if (np >= ep) {
  584.         seterror(ERR_SELOVFL);
  585.         addla(name);
  586.         return;
  587.     }
  588.     c = getC(DOEXCL);
  589.     }
  590.     /*
  591.      * Name up to here is a max of 2 * MAXVARLEN + 8.
  592.      */
  593.     if (c == ':') {
  594.     /*
  595.      * if the :g modifier is followed by a newline, then error right away!
  596.      * -strike
  597.      */
  598.  
  599.     int     gmodflag = 0;
  600.  
  601. #ifndef COMPAT
  602.     do {
  603. #endif /* COMPAT */
  604.         *np++ = c, c = getC(DOEXCL);
  605.         if (c == 'g')
  606.         gmodflag++, *np++ = c, c = getC(DOEXCL);
  607.         *np++ = c;
  608.         if (!any("htrqxe", c)) {
  609.         if (gmodflag && c == '\n')
  610.             stderror(ERR_VARSYN);    /* strike */
  611.         seterror(ERR_VARMOD, c);
  612.         *np = 0;
  613.         addla(name);
  614.         return;
  615.         }
  616. #ifndef COMPAT
  617.     }
  618.     while ((c = getC(DOEXCL)) == ':');
  619.     ungetD(c);
  620. #endif /* COMPAT */
  621.     }
  622.     else
  623.     ungetD(c);
  624.     if (sc == '{') {
  625.     c = getC(DOEXCL);
  626.     if (c != '}') {
  627.         ungetD(c);
  628.         seterror(ERR_MISSING, '}');
  629.         *np = 0;
  630.         addla(name);
  631.         return;
  632.     }
  633.     *np++ = c;
  634.     }
  635.     *np = 0;
  636.     addla(name);
  637.     return;
  638. }
  639.  
  640. void
  641. addla(cp)
  642.     Char   *cp;
  643. {
  644.     Char    buf[BUFSIZ];
  645.  
  646.     if (Strlen(cp) + (lap ? Strlen(lap) : 0) >=
  647.     (sizeof(labuf) - 4) / sizeof(Char)) {
  648.     seterror(ERR_EXPOVFL);
  649.     return;
  650.     }
  651.     if (lap)
  652.     (void) Strcpy(buf, lap);
  653.     (void) Strcpy(labuf, cp);
  654.     if (lap)
  655.     (void) Strcat(labuf, buf);
  656.     lap = labuf;
  657. }
  658.  
  659. static Char lhsb[32];
  660. static Char slhs[32];
  661. static Char rhsb[64];
  662. static int quesarg;
  663.  
  664. static void
  665. getexcl(sc)
  666.     int    sc;
  667. {
  668.     register struct wordent *hp, *ip;
  669.     int     left, right, dol;
  670.     register int c;
  671.  
  672.     if (sc == 0) {
  673.     sc = getC(0);
  674.     if (sc != '{') {
  675.         ungetC(sc);
  676.         sc = 0;
  677.     }
  678.     }
  679.     quesarg = -1;
  680.     lastev = eventno;
  681.     hp = gethent(sc);
  682.     if (hp == 0)
  683.     return;
  684.     hadhist = 1;
  685.     dol = 0;
  686.     if (hp == alhistp)
  687.     for (ip = hp->next->next; ip != alhistt; ip = ip->next)
  688.         dol++;
  689.     else
  690.     for (ip = hp->next->next; ip != hp->prev; ip = ip->next)
  691.         dol++;
  692.     left = 0, right = dol;
  693.     if (sc == HISTSUB) {
  694.     ungetC('s'), unreadc(HISTSUB), c = ':';
  695.     goto subst;
  696.     }
  697.     c = getC(0);
  698.     if (!any(":^$*-%", c))
  699.     goto subst;
  700.     left = right = -1;
  701.     if (c == ':') {
  702.     c = getC(0);
  703.     unreadc(c);
  704.     if (letter(c) || c == '&') {
  705.         c = ':';
  706.         left = 0, right = dol;
  707.         goto subst;
  708.     }
  709.     }
  710.     else
  711.     ungetC(c);
  712.     if (!getsel(&left, &right, dol))
  713.     return;
  714.     c = getC(0);
  715.     if (c == '*')
  716.     ungetC(c), c = '-';
  717.     if (c == '-') {
  718.     if (!getsel(&left, &right, dol))
  719.         return;
  720.     c = getC(0);
  721.     }
  722. subst:
  723.     exclc = right - left + 1;
  724.     while (--left >= 0)
  725.     hp = hp->next;
  726.     if (sc == HISTSUB || c == ':') {
  727.     do {
  728.         hp = getsub(hp);
  729.         c = getC(0);
  730.     } while (c == ':');
  731.     }
  732.     unreadc(c);
  733.     if (sc == '{') {
  734.     c = getC(0);
  735.     if (c != '}')
  736.         seterror(ERR_BADBANG);
  737.     }
  738.     exclnxt = hp;
  739. }
  740.  
  741. static struct wordent *
  742. getsub(en)
  743.     struct wordent *en;
  744. {
  745.     register Char *cp;
  746.     int     delim;
  747.     register int c;
  748.     int     sc;
  749.     bool global;
  750.     Char    orhsb[sizeof(rhsb) / sizeof(Char)];
  751.  
  752. #ifndef COMPAT
  753.     do {
  754. #endif /* COMPAT */
  755.     exclnxt = 0;
  756.     global = 0;
  757.     sc = c = getC(0);
  758.     if (c == 'g')
  759.         global++, sc = c = getC(0);
  760.  
  761.     switch (c) {
  762.     case 'p':
  763.         justpr++;
  764.         return (en);
  765.  
  766.     case 'x':
  767.     case 'q':
  768.         global++;
  769.  
  770.         /* fall into ... */
  771.  
  772.     case 'h':
  773.     case 'r':
  774.     case 't':
  775.     case 'e':
  776.         break;
  777.  
  778.     case '&':
  779.         if (slhs[0] == 0) {
  780.         seterror(ERR_NOSUBST);
  781.         return (en);
  782.         }
  783.         (void) Strcpy(lhsb, slhs);
  784.         break;
  785.  
  786. #ifdef notdef
  787.     case '~':
  788.         if (lhsb[0] == 0)
  789.         goto badlhs;
  790.         break;
  791. #endif
  792.  
  793.     case 's':
  794.         delim = getC(0);
  795.         if (letter(delim) || Isdigit(delim) || any(" \t\n", delim)) {
  796.         unreadc(delim);
  797.         lhsb[0] = 0;
  798.         seterror(ERR_BADSUBST);
  799.         return (en);
  800.         }
  801.         cp = lhsb;
  802.         for (;;) {
  803.         c = getC(0);
  804.         if (c == '\n') {
  805.             unreadc(c);
  806.             break;
  807.         }
  808.         if (c == delim)
  809.             break;
  810.         if (cp > &lhsb[sizeof(lhsb) / sizeof(Char) - 2]) {
  811.             lhsb[0] = 0;
  812.             seterror(ERR_BADSUBST);
  813.             return (en);
  814.         }
  815.         if (c == escchar) {
  816.             c = getC(0);
  817.             if (c != delim && c != escchar)
  818.             *cp++ = escchar;
  819.         }
  820.         *cp++ = c;
  821.         }
  822.         if (cp != lhsb)
  823.         *cp++ = 0;
  824.         else if (lhsb[0] == 0) {
  825.         seterror(ERR_LHS);
  826.         return (en);
  827.         }
  828.         cp = rhsb;
  829.         (void) Strcpy(orhsb, cp);
  830.         for (;;) {
  831.         c = getC(0);
  832.         if (c == '\n') {
  833.             unreadc(c);
  834.             break;
  835.         }
  836.         if (c == delim)
  837.             break;
  838. #ifdef notdef
  839.         if (c == '~') {
  840.             if (&cp[Strlen(orhsb)] > &rhsb[sizeof(rhsb) /
  841.                            sizeof(Char) - 2])
  842.             goto toorhs;
  843.             (void) Strcpy(cp, orhsb);
  844.             cp = Strend(cp);
  845.             continue;
  846.         }
  847. #endif
  848.         if (cp > &rhsb[sizeof(rhsb) / sizeof(Char) - 2]) {
  849.             seterror(ERR_RHSLONG);
  850.             return (en);
  851.         }
  852.         if (c == escchar) {
  853.             c = getC(0);
  854.             if (c != delim /* && c != '~' */ )
  855.             *cp++ = escchar;
  856.         }
  857.         *cp++ = c;
  858.         }
  859.         *cp++ = 0;
  860.         break;
  861.  
  862.     default:
  863.         if (c == '\n')
  864.         unreadc(c);
  865.         seterror(ERR_BADBANGMOD, c);
  866.         return (en);
  867.     }
  868.     (void) Strcpy(slhs, lhsb);
  869.     if (exclc)
  870.         en = dosub(sc, en, global);
  871. #ifndef COMPAT
  872.     }
  873.     while ((c = getC(0)) == ':');
  874.     unreadc(c);
  875. #endif /* COMPAT */
  876.     return (en);
  877. }
  878.  
  879. static struct wordent *
  880. dosub(sc, en, global)
  881.     int     sc;
  882.     struct wordent *en;
  883.     bool global;
  884. {
  885.     struct wordent lexi;
  886.     bool    didsub = 0;
  887.     struct wordent *hp = &lexi;
  888.     register struct wordent *wdp;
  889.     register int i = exclc;
  890.  
  891.     wdp = hp;
  892.     while (--i >= 0) {
  893.     register struct wordent *new = (struct wordent *)
  894.     xcalloc(1, sizeof *wdp);
  895.  
  896.     new->word = 0;
  897.     new->prev = wdp;
  898.     new->next = hp;
  899.     wdp->next = new;
  900.     wdp = new;
  901.     en = en->next;
  902.     wdp->word = (en->word && (global ||didsub == 0)) ?
  903.         subword(en->word, sc, &didsub) : Strsave(en->word);
  904.     }
  905.     if (didsub == 0)
  906.     seterror(ERR_MODFAIL);
  907.     hp->prev = wdp;
  908.     return (&enthist(-1000, &lexi, 0)->Hlex);
  909. }
  910.  
  911. static Char *
  912. subword(cp, type, adid)
  913.     Char   *cp;
  914.     int     type;
  915.     bool   *adid;
  916. {
  917.     Char    wbuf[BUFSIZ];
  918.     register Char *wp, *mp, *np;
  919.     register int i;
  920.  
  921.     switch (type) {
  922.  
  923.     case 'r':
  924.     case 'e':
  925.     case 'h':
  926.     case 't':
  927.     case 'q':
  928.     case 'x':
  929.     wp = domod(cp, type);
  930.     if (wp == 0)
  931.         return (Strsave(cp));
  932.     *adid = 1;
  933.     return (wp);
  934.  
  935.     default:
  936.     wp = wbuf;
  937.     i = BUFSIZ - 4;
  938.     for (mp = cp; *mp; mp++)
  939.         if (matchs(mp, lhsb)) {
  940.         for (np = cp; np < mp;)
  941.             *wp++ = *np++, --i;
  942.         for (np = rhsb; *np; np++)
  943.             switch (*np) {
  944.  
  945.             case '\\':
  946.             if (np[1] == '&')
  947.                 np++;
  948.             /* fall into ... */
  949.  
  950.             default:
  951.             if (--i < 0) {
  952.                 seterror(ERR_SUBOVFL);
  953.                 return (STRNULL);
  954.             }
  955.             *wp++ = *np;
  956.             continue;
  957.  
  958.             case '&':
  959.             i -= Strlen(lhsb);
  960.             if (i < 0) {
  961.                 seterror(ERR_SUBOVFL);
  962.                 return (STRNULL);
  963.             }
  964.             *wp = 0;
  965.             (void) Strcat(wp, lhsb);
  966.             wp = Strend(wp);
  967.             continue;
  968.             }
  969.         mp += Strlen(lhsb);
  970.         i -= Strlen(mp);
  971.         if (i < 0) {
  972.             seterror(ERR_SUBOVFL);
  973.             return (STRNULL);
  974.         }
  975.         *wp = 0;
  976.         (void) Strcat(wp, mp);
  977.         *adid = 1;
  978.         return (Strsave(wbuf));
  979.         }
  980.     return (Strsave(cp));
  981.     }
  982. }
  983.  
  984. Char   *
  985. domod(cp, type)
  986.     Char   *cp;
  987.     int     type;
  988. {
  989.     register Char *wp, *xp;
  990.     register int c;
  991.  
  992.     switch (type) {
  993.  
  994.     case 'x':
  995.     case 'q':
  996.     wp = Strsave(cp);
  997.     for (xp = wp; c = *xp; xp++)
  998.         if ((c != ' ' && c != '\t') || type == 'q')
  999.         *xp |= QUOTE;
  1000.     return (wp);
  1001.  
  1002.     case 'h':
  1003.     case 't':
  1004.     if (!any(short2str(cp), '/'))
  1005.         return (type == 't' ? Strsave(cp) : 0);
  1006.     wp = Strend(cp);
  1007.     while (*--wp != '/')
  1008.         continue;
  1009.     if (type == 'h')
  1010.         xp = Strsave(cp), xp[wp - cp] = 0;
  1011.     else
  1012.         xp = Strsave(wp + 1);
  1013.     return (xp);
  1014.  
  1015.     case 'e':
  1016.     case 'r':
  1017.     wp = Strend(cp);
  1018.     for (wp--; wp >= cp && *wp != '/'; wp--)
  1019.         if (*wp == '.') {
  1020.         if (type == 'e')
  1021.             xp = Strsave(wp + 1);
  1022.         else
  1023.             xp = Strsave(cp), xp[wp - cp] = 0;
  1024.         return (xp);
  1025.         }
  1026.     return (Strsave(type == 'e' ? STRNULL : cp));
  1027.     }
  1028.     return (0);
  1029. }
  1030.  
  1031. static int
  1032. matchs(str, pat)
  1033.     register Char *str, *pat;
  1034. {
  1035.     while (*str && *pat && *str == *pat)
  1036.     str++, pat++;
  1037.     return (*pat == 0);
  1038. }
  1039.  
  1040. static int
  1041. getsel(al, ar, dol)
  1042.     register int *al, *ar;
  1043.     int     dol;
  1044. {
  1045.     register int c = getC(0);
  1046.     register int i;
  1047.     bool    first = *al < 0;
  1048.  
  1049.     switch (c) {
  1050.  
  1051.     case '%':
  1052.     if (quesarg == -1) {
  1053.         seterror(ERR_BADBANGARG);
  1054.         return (0);
  1055.     }
  1056.     if (*al < 0)
  1057.         *al = quesarg;
  1058.     *ar = quesarg;
  1059.     break;
  1060.  
  1061.     case '-':
  1062.     if (*al < 0) {
  1063.         *al = 0;
  1064.         *ar = dol - 1;
  1065.         unreadc(c);
  1066.     }
  1067.     return (1);
  1068.  
  1069.     case '^':
  1070.     if (*al < 0)
  1071.         *al = 1;
  1072.     *ar = 1;
  1073.     break;
  1074.  
  1075.     case '$':
  1076.     if (*al < 0)
  1077.         *al = dol;
  1078.     *ar = dol;
  1079.     break;
  1080.  
  1081.     case '*':
  1082.     if (*al < 0)
  1083.         *al = 1;
  1084.     *ar = dol;
  1085.     if (*ar < *al) {
  1086.         *ar = 0;
  1087.         *al = 1;
  1088.         return (1);
  1089.     }
  1090.     break;
  1091.  
  1092.     default:
  1093.     if (Isdigit(c)) {
  1094.         i = 0;
  1095.         while (Isdigit(c)) {
  1096.         i = i * 10 + c - '0';
  1097.         c = getC(0);
  1098.         }
  1099.         if (i < 0)
  1100.         i = dol + 1;
  1101.         if (*al < 0)
  1102.         *al = i;
  1103.         *ar = i;
  1104.     }
  1105.     else if (*al < 0)
  1106.         *al = 0, *ar = dol;
  1107.     else
  1108.         *ar = dol - 1;
  1109.     unreadc(c);
  1110.     break;
  1111.     }
  1112.     if (first) {
  1113.     c = getC(0);
  1114.     unreadc(c);
  1115.     if (any("-$*", c))
  1116.         return (1);
  1117.     }
  1118.     if (*al > *ar || *ar > dol) {
  1119.     seterror(ERR_BADBANGARG);
  1120.     return (0);
  1121.     }
  1122.     return (1);
  1123.  
  1124. }
  1125.  
  1126. static struct wordent *
  1127. gethent(sc)
  1128.     int     sc;
  1129. {
  1130.     register struct Hist *hp;
  1131.     register Char *np;
  1132.     register int c;
  1133.     int     event;
  1134.     bool    back = 0;
  1135.  
  1136.     c = sc == HISTSUB ? HIST : getC(0);
  1137.     if (c == HIST) {
  1138.     if (alhistp)
  1139.         return (alhistp);
  1140.     event = eventno;
  1141.     }
  1142.     else
  1143.     switch (c) {
  1144.  
  1145.     case ':':
  1146.     case '^':
  1147.     case '$':
  1148.     case '*':
  1149.     case '%':
  1150.         ungetC(c);
  1151.         if (lastev == eventno && alhistp)
  1152.         return (alhistp);
  1153.         event = lastev;
  1154.         break;
  1155.  
  1156.     case '#':        /* !# is command being typed in (mrh) */
  1157.         if (--hleft == 0) {
  1158.         seterror(ERR_HISTLOOP);
  1159.         return (0);
  1160.         }
  1161.         else
  1162.         return (¶ml);
  1163.         /* NOTREACHED */
  1164.  
  1165.     case '-':
  1166.         back = 1;
  1167.         c = getC(0);
  1168.         /* FALLSTHROUGH */
  1169.  
  1170.     default:
  1171.         if (any("(=~", c)) {
  1172.         unreadc(c);
  1173.         ungetC(HIST);
  1174.         return (0);
  1175.         }
  1176.         np = lhsb;
  1177.         event = 0;
  1178.         while (!any(": \t\\\n}", c)) {
  1179.         if (event != -1 && Isdigit(c))
  1180.             event = event * 10 + c - '0';
  1181.         else
  1182.             event = -1;
  1183.         if (np < &lhsb[sizeof(lhsb) / sizeof(Char) - 2])
  1184.             *np++ = c;
  1185.         c = getC(0);
  1186.         }
  1187.         unreadc(c);
  1188.         if (np == lhsb) {
  1189.         ungetC(HIST);
  1190.         return (0);
  1191.         }
  1192.         *np++ = 0;
  1193.         if (event != -1) {
  1194.         /*
  1195.          * History had only digits
  1196.          */
  1197.         if (back)
  1198.             event = eventno + (alhistp == 0) - (event ? event : 0);
  1199.         break;
  1200.         }
  1201.         hp = findev(lhsb, 0);
  1202.         if (hp)
  1203.         lastev = hp->Hnum;
  1204.         return (&hp->Hlex);
  1205.  
  1206.     case '?':
  1207.         np = lhsb;
  1208.         for (;;) {
  1209.         c = getC(0);
  1210.         if (c == '\n') {
  1211.             unreadc(c);
  1212.             break;
  1213.         }
  1214.         if (c == '?')
  1215.             break;
  1216.         if (np < &lhsb[sizeof(lhsb) / sizeof(Char) - 2])
  1217.             *np++ = c;
  1218.         }
  1219.         if (np == lhsb) {
  1220.         if (lhsb[0] == 0) {
  1221.             seterror(ERR_NOSEARCH);
  1222.             return (0);
  1223.         }
  1224.         }
  1225.         else
  1226.         *np++ = 0;
  1227.         hp = findev(lhsb, 1);
  1228.         if (hp)
  1229.         lastev = hp->Hnum;
  1230.         return (&hp->Hlex);
  1231.     }
  1232.  
  1233.     for (hp = Histlist.Hnext; hp; hp = hp->Hnext)
  1234.     if (hp->Hnum == event) {
  1235.         hp->Href = eventno;
  1236.         lastev = hp->Hnum;
  1237.         return (&hp->Hlex);
  1238.     }
  1239.     np = putn(event);
  1240.     seterror(ERR_NOEVENT, short2str(np));
  1241.     return (0);
  1242. }
  1243.  
  1244. static struct Hist *
  1245. findev(cp, anyarg)
  1246.     Char   *cp;
  1247.     bool    anyarg;
  1248. {
  1249.     register struct Hist *hp;
  1250.  
  1251.     for (hp = Histlist.Hnext; hp; hp = hp->Hnext) {
  1252.     Char   *dp;
  1253.     register Char *p, *q;
  1254.     register struct wordent *lp = hp->Hlex.next;
  1255.     int     argno = 0;
  1256.  
  1257.     /*
  1258.      * The entries added by alias substitution don't have a newline but do
  1259.      * have a negative event number. Savehist() trims off these entries,
  1260.      * but it happens before alias expansion, too early to delete those
  1261.      * from the previous command.
  1262.      */
  1263.     if (hp->Hnum < 0)
  1264.         continue;
  1265.     if (lp->word[0] == '\n')
  1266.         continue;
  1267.     if (!anyarg) {
  1268.         p = cp;
  1269.         q = lp->word;
  1270.         do
  1271.         if (!*p)
  1272.             return (hp);
  1273.         while (*p++ == *q++);
  1274.         continue;
  1275.     }
  1276.     do {
  1277.         for (dp = lp->word; *dp; dp++) {
  1278.         p = cp;
  1279.         q = dp;
  1280.         do
  1281.             if (!*p) {
  1282.             quesarg = argno;
  1283.             return (hp);
  1284.             }
  1285.         while (*p++ == *q++);
  1286.         }
  1287.         lp = lp->next;
  1288.         argno++;
  1289.     } while (lp->word[0] != '\n');
  1290.     }
  1291.     seterror(ERR_NOEVENT, short2str(cp));
  1292.     return (0);
  1293. }
  1294.  
  1295.  
  1296. static void
  1297. setexclp(cp)
  1298.     register Char *cp;
  1299. {
  1300.     if (cp && cp[0] == '\n')
  1301.     return;
  1302.     exclp = cp;
  1303. }
  1304.  
  1305. void
  1306. unreadc(c)
  1307.     int    c;
  1308. {
  1309.     peekread = c;
  1310. }
  1311.  
  1312. int
  1313. readc(wanteof)
  1314.     bool    wanteof;
  1315. {
  1316.     register int c;
  1317.     static  sincereal;
  1318.  
  1319.     if (c = peekread) {
  1320.     peekread = 0;
  1321.     return (c);
  1322.     }
  1323. top:
  1324.     if (alvecp) {
  1325.     if (c = *alvecp++)
  1326.         return (c);
  1327.     if (*alvec) {
  1328.         alvecp = *alvec++;
  1329.         return (' ');
  1330.     }
  1331.     }
  1332.     if (alvec) {
  1333.     if (alvecp = *alvec) {
  1334.         alvec++;
  1335.         goto top;
  1336.     }
  1337.     /* Infinite source! */
  1338.     return ('\n');
  1339.     }
  1340.     if (evalp) {
  1341.     if (c = *evalp++)
  1342.         return (c);
  1343.     if (*evalvec) {
  1344.         evalp = *evalvec++;
  1345.         return (' ');
  1346.     }
  1347.     evalp = 0;
  1348.     }
  1349.     if (evalvec) {
  1350.     if (evalvec == (Char **) 1) {
  1351.         doneinp = 1;
  1352.         reset();
  1353.     }
  1354.     if (evalp = *evalvec) {
  1355.         evalvec++;
  1356.         goto top;
  1357.     }
  1358.     evalvec = (Char **) 1;
  1359.     return ('\n');
  1360.     }
  1361.     do {
  1362.     if (arginp == (Char *) 1 || onelflg == 1) {
  1363.         if (wanteof)
  1364.         return (-1);
  1365.         exitstat();
  1366.     }
  1367.     if (arginp) {
  1368.         if ((c = *arginp++) == 0) {
  1369.         arginp = (Char *) 1;
  1370.         return ('\n');
  1371.         }
  1372.         return (c);
  1373.     }
  1374. reread:
  1375.     c = bgetc();
  1376.     if (c < 0) {
  1377. #ifndef POSIX
  1378. #ifdef TERMIO
  1379. #include <termio.h>
  1380.         struct termio tty;
  1381.  
  1382. #else                /* SGTTYB */
  1383. #include <sys/ioctl.h>
  1384.         struct sgttyb tty;
  1385.  
  1386. #endif                /* TERMIO */
  1387. #else                /* POSIX */
  1388. #include <termios.h>
  1389.         struct termios tty;
  1390.  
  1391. #endif                /* POSIX */
  1392.         if (wanteof)
  1393.         return (-1);
  1394.         /* was isatty but raw with ignoreeof yields problems */
  1395. #ifndef POSIX
  1396. #ifdef TERMIO
  1397.         if (ioctl(SHIN, TCGETA, (ioctl_t) & tty) == 0 &&
  1398.         (tty.c_lflag & ICANON))
  1399. #else                /* GSTTYB */
  1400.         if (ioctl(SHIN, TIOCGETP, (ioctl_t) & tty) == 0 &&
  1401.         (tty.sg_flags & RAW) == 0)
  1402. #endif                /* TERMIO */
  1403. #else                /* POSIX */
  1404.         if (tcgetattr(SHIN, &tty) == 0 &&
  1405.         (tty.c_lflag & ICANON))
  1406. #endif                /* POSIX */
  1407.         {
  1408.         /* was 'short' for FILEC */
  1409.         int     ctpgrp;
  1410.  
  1411.         if (++sincereal > 25)
  1412.             goto oops;
  1413. #ifdef BSDJOBS
  1414.         if (tpgrp != -1 &&
  1415.             (ctpgrp = tcgetpgrp(FSHTTY)) != -1 &&
  1416.             tpgrp != ctpgrp) {
  1417.             (void) tcsetpgrp(FSHTTY, tpgrp);
  1418.             (void) killpg((pid_t) ctpgrp, SIGHUP);
  1419.             xprintf("Reset tty pgrp from %d to %d\n", ctpgrp, tpgrp);
  1420.             goto reread;
  1421.         }
  1422. #endif                /* BSDJOBS */
  1423.         if (adrof(STRignoreeof)) {
  1424.             if (loginsh)
  1425.             xprintf("\nUse \"logout\" to logout.\n");
  1426.             else
  1427.             xprintf("\nUse \"exit\" to leave tcsh.\n");
  1428.             reset();
  1429.         }
  1430.         if (chkstop == 0)
  1431.             panystop(1);
  1432.         }
  1433.     oops:
  1434.         doneinp = 1;
  1435.         reset();
  1436.     }
  1437.     sincereal = 0;
  1438.     if (c == '\n' && onelflg)
  1439.         onelflg--;
  1440.     } while (c == 0);
  1441.     if (histlinep < histline + BUFSIZ)
  1442.     *histlinep++ = c;
  1443.     return (c);
  1444. }
  1445.  
  1446. static int
  1447. bgetc()
  1448. {
  1449.     register int buf, off;
  1450.     int c;
  1451.     register int numleft = 0, roomleft;
  1452.     extern Char InputBuf[];
  1453.     char    tbuf[BUFSIZ + 1];
  1454.  
  1455.     if (cantell) {
  1456.     if (fseekp < fbobp || fseekp > feobp) {
  1457.         fbobp = feobp = fseekp;
  1458.         (void) lseek(SHIN, fseekp, L_SET);
  1459.     }
  1460.     if (fseekp == feobp) {
  1461.         int     i;
  1462.  
  1463.         fbobp = feobp;
  1464.         do
  1465.         c = read(SHIN, tbuf, BUFSIZ);
  1466.         while (c < 0 && errno == EINTR);
  1467.         if (c <= 0)
  1468.         return (-1);
  1469.         for (i = 0; i < c; i++)
  1470.         fbuf[0][i] = (unsigned char) tbuf[i];
  1471.         feobp += c;
  1472.     }
  1473.     c = fbuf[0][fseekp - fbobp];
  1474.     fseekp++;
  1475. #ifdef __MINT__
  1476.     return (c == '\r' ? bgetc() : c);
  1477. #else
  1478.     return (c);
  1479. #endif
  1480.     }
  1481. again:
  1482.     buf = (int) fseekp / BUFSIZ;
  1483.     if (buf >= fblocks) {
  1484.     register Char **nfbuf =
  1485.     (Char **) xcalloc((size_t) (fblocks + 2),
  1486.               sizeof(Char **));
  1487.  
  1488.     if (fbuf) {
  1489.         (void) blkcpy(nfbuf, fbuf);
  1490.         xfree((ptr_t) fbuf);
  1491.     }
  1492.     fbuf = nfbuf;
  1493.     fbuf[fblocks] = (Char *) xcalloc(BUFSIZ, sizeof(Char));
  1494.     fblocks++;
  1495.     if (!intty)
  1496.         goto again;
  1497.     }
  1498.     if (fseekp >= feobp) {
  1499.     buf = (int) feobp / BUFSIZ;
  1500.     off = (int) feobp % BUFSIZ;
  1501.     roomleft = BUFSIZ - off;
  1502.     for (;;) {
  1503.         if (editing && intty) {    /* then use twenex routine */
  1504.         c = numleft ? numleft : Inputl();    /* PWP: get a line */
  1505.         if (c > roomleft) {    /* No room in this buffer? */
  1506.             /* start with fresh buffer */
  1507.             feobp = fseekp = fblocks * BUFSIZ;
  1508.             numleft = c;
  1509.             goto again;
  1510.         }
  1511.         if (c > 0)
  1512.             copy((char *) (fbuf[buf] + off),
  1513.              (char *) InputBuf, (int) (c * sizeof(Char)));
  1514.         /* copy (fbuf[buf] + off, ttyline, c); */
  1515.         numleft = 0;
  1516.         }
  1517.         else {
  1518.         c = read(SHIN, tbuf, (size_t) roomleft);
  1519.         if (c > 0) {
  1520.             int     i;
  1521.             Char   *ptr = fbuf[buf] + off;
  1522.  
  1523.             for (i = 0; i < c; i++)
  1524.             ptr[i] = (unsigned char) tbuf[i];
  1525.         }
  1526.         }
  1527.         if (c >= 0)
  1528.         break;
  1529.         switch (errno) {
  1530. #ifdef EWOULDBLOCK
  1531.         case EWOULDBLOCK:
  1532. # define TRY_AGAIN
  1533. #endif /* EWOULDBLOCK */
  1534. #if defined(POSIX) && defined(EAGAIN)
  1535. # if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
  1536.         case EAGAIN:
  1537. #  define TRY_AGAIN
  1538. # endif /* EWOULDBLOCK && EWOULDBLOCK != EAGAIN */
  1539. #endif /* POSIX && EAGAIN */
  1540. #ifdef TRY_AGAIN
  1541. # if defined(F_SETFL) && defined(O_NDELAY)
  1542.         (void) fcntl(SHIN, F_SETFL, fcntl(SHIN,F_GETFL,0) & ~O_NDELAY);
  1543. # endif /* F_SETFL && O_NDELAY */
  1544. # ifdef FIONBIO
  1545.         c = 0;
  1546.         (void) ioctl(SHIN, FIONBIO, (ioctl_t) &c);
  1547. # endif    /* FIONBIO */
  1548. # if (defined(F_SETFL) && defined(O_NDELAY)) || defined(FIONBIO)
  1549.         c = 0;
  1550. # endif    /* (F_SETFL && O_NDELAY) || FIONBIO */
  1551.         break;
  1552. #endif /* TRY_AGAIN */
  1553. #ifdef _SEQUENT_
  1554.         case EBADF:
  1555. #endif    /* _SEQUENT_ */
  1556.         case EINTR:
  1557.         c = 0;
  1558.         break;
  1559.         default:
  1560.         c = -1;
  1561.         break;
  1562.         }
  1563.         if (c == -1)
  1564.         break;
  1565.     }
  1566.     if (c <= 0)
  1567.         return (-1);
  1568.     feobp += c;
  1569.     if (editing && !intty)
  1570.         goto again;
  1571.     }
  1572.     c = fbuf[buf][(int) fseekp % BUFSIZ];
  1573.     fseekp++;
  1574. #ifdef __MINT__
  1575.     return (c == '\r' ? bgetc() : c);
  1576. #else
  1577.     return (c);
  1578. #endif
  1579. }
  1580.  
  1581. static void
  1582. bfree()
  1583. {
  1584.     register int sb, i;
  1585.  
  1586.     if (cantell)
  1587.     return;
  1588.     if (whyles)
  1589.     return;
  1590.     sb = (int) (fseekp - 1) / BUFSIZ;
  1591.     if (sb > 0) {
  1592.     for (i = 0; i < sb; i++)
  1593.         xfree((ptr_t) fbuf[i]);
  1594.     (void) blkcpy(fbuf, &fbuf[sb]);
  1595.     fseekp -= BUFSIZ * sb;
  1596.     feobp -= BUFSIZ * sb;
  1597.     fblocks -= sb;
  1598.     }
  1599. }
  1600.  
  1601. void
  1602. bseek(l)
  1603.     off_t   l;
  1604. {
  1605.  
  1606.     fseekp = l;
  1607.     if (!cantell) {
  1608. #ifdef notdef
  1609.     register struct whyle *wp;
  1610. #endif
  1611.     if (!whyles)
  1612.         return;
  1613. #ifdef notdef
  1614.     /*
  1615.      * Christos: I don't understand this? both wp and l are local. What is
  1616.      * this used for? I suspect the author meant fseek = wp->w_start
  1617.      */
  1618.     for (wp = whyles; wp->w_next; wp = wp->w_next)
  1619.         continue;
  1620.     if (wp->w_start > l)
  1621.         l = wp->w_start;
  1622. #endif
  1623.     }
  1624. }
  1625.  
  1626. /* any similarity to bell telephone is purely accidental */
  1627. #ifndef btell
  1628. off_t
  1629. btell()
  1630. {
  1631.     return (fseekp);
  1632. }
  1633.  
  1634. #endif
  1635.  
  1636. void
  1637. btoeof()
  1638. {
  1639.     (void) lseek(SHIN, (off_t) 0, L_XTND);
  1640.     fseekp = feobp;
  1641.     wfree();
  1642.     bfree();
  1643. }
  1644.  
  1645. #ifdef __MINT__
  1646. #define ESPIPE EINVAL
  1647. #endif
  1648.  
  1649. void
  1650. settell()
  1651. {
  1652.     cantell = 0;
  1653.     if (arginp || onelflg || intty)
  1654.     return;
  1655.     if (lseek(SHIN, (off_t) 0, L_INCR) < 0 || errno == ESPIPE)
  1656.     return;
  1657.     fbuf = (Char **) xcalloc(2, sizeof(Char **));
  1658.     fblocks = 1;
  1659.     fbuf[0] = (Char *) xcalloc(BUFSIZ, sizeof(Char));
  1660.     fseekp = fbobp = feobp = lseek(SHIN, (off_t) 0, L_INCR);
  1661.     cantell = 1;
  1662. }
  1663.